import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import { cloneDeep } from "lodash";
import GlobalData, { UserInfo } from "@/interface/globalData";

const initData = {
  userInfo: {
    userId: "",
    token: "",
    realName: "",
  },
};

const useData = create<GlobalData>()(
  persist(
    (set) => ({
      ...cloneDeep(initData),
      setUserInfo: (userInfo: UserInfo) => set(() => ({ userInfo })),
    }),
    {
      name: "admin-storage",
      storage: createJSONStorage(() => sessionStorage),
    },
  ),
);

export default useData;
